package com.lzy.system.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.exception.BusException;
import com.lzy.system.organization.dto.SysDepartmentDTO;
import com.lzy.system.organization.dto.SysDepartmentOrderDTO;
import com.lzy.system.organization.dto.convert.SysDepartmentConvert;
import com.lzy.system.organization.entity.SysCompany;
import com.lzy.system.organization.entity.SysDepartment;
import com.lzy.system.organization.mapper.SysDepartmentMapper;
import com.lzy.system.organization.service.ISysCompanyService;
import com.lzy.system.organization.service.ISysDepartmentService;
import com.lzy.system.organization.vo.SysDepartmentVo;
import com.lzy.utils.BeanMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2023-11-22
 */
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements ISysDepartmentService {
    ISysCompanyService iSysCompanyService;
    SysDepartmentConvert sysDepartmentConvert;

    @Autowired
    public SysDepartmentServiceImpl(ISysCompanyService iSysCompanyService, SysDepartmentConvert sysDepartmentConvert) {
        this.iSysCompanyService = iSysCompanyService;
        this.sysDepartmentConvert = sysDepartmentConvert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysDepartmentDTO sysDepartmentDTO) throws BusException {
        SysDepartment sysDepartment = (SysDepartment) BeanMapHelper.Bean2Bean(sysDepartmentDTO, SysDepartment.class);
        if (sysDepartment.getPid() == null) {
            sysDepartment.setPid(0L);
        }
        return this.save(sysDepartment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDepartmentDTO sysDepartmentDTO) throws BusException {
        SysDepartment sysDepartment = (SysDepartment) BeanMapHelper.Bean2Bean(sysDepartmentDTO, SysDepartment.class);
        return this.updateById(sysDepartment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean del(List<SysDepartment> sysDepartments) throws BusException {
        List<Long> ids = new ArrayList<>();
        class Recursion {
            public void getDepSub(Long pid, List<Long> ids) {
                List<SysDepartment> list = list(new QueryWrapper() {{
                    eq("pid", pid);
                }});
                if (list.size() == 0) {
                    return;
                }
                for (SysDepartment sysDepartment : list) {
                    ids.add(sysDepartment.getId());
                    getDepSub(sysDepartment.getId(), ids);
                }
            }
        }
        for (SysDepartment sysDepartment : sysDepartments) {
            ids.add(sysDepartment.getId());
            new Recursion().getDepSub(sysDepartment.getId(), ids);
        }
        return removeBatchByIds(ids);
    }

    @Override
    public List<SysDepartment> getList() throws BusException {
        return list();
    }

    @Override
    public List<SysDepartmentVo> getListByPid(Long pid, Integer companyId) throws BusException {
        QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", pid);

        //查询默认公司
        if (companyId == null) {
            SysCompany sysCompany_def = iSysCompanyService.getOne(new QueryWrapper() {{
                eq("is_def", 1);
            }});
            if (sysCompany_def == null) {
                sysCompany_def = iSysCompanyService.getOne(new QueryWrapper() {{
                    last("limit 1");
                }});
            }
            if (sysCompany_def == null) {
                throw new BusException("没有公司请先维护公司");
            }
            companyId = sysCompany_def.getId();
        }
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("company_id", companyId);
        queryWrapper.orderByAsc("order_index").orderByAsc("id");

        List<SysDepartment> dep_list = list(queryWrapper);
        List<SysDepartmentVo> list = new ArrayList<>();
        for (SysDepartment sysDepartment : dep_list) {
            SysDepartmentVo sysDepartmentVo = (SysDepartmentVo) BeanMapHelper.Bean2Bean(sysDepartment, SysDepartmentVo.class);
            list.add(sysDepartmentVo);
        }
        for (SysDepartmentVo sysDepartmentVo : list) {
            long count = count(new QueryWrapper() {{
                eq("pid", sysDepartmentVo.getId());
            }});
            if (count > 0) {
                sysDepartmentVo.setIsParent(true);
            } else {
                sysDepartmentVo.setIsParent(false);
            }
        }
        return list;
    }

    @Override
    public void saveOrder(List<SysDepartmentOrderDTO> sysDepartmentOrderDTOS) throws BusException {
        List<SysDepartment> sysDepartments = sysDepartmentConvert.orderDTO_to_SysDepartment_list(sysDepartmentOrderDTOS);
        boolean b = this.updateBatchById(sysDepartments);
        if (!b) {
            throw new BusException("保存失败");
        }
    }
}
